# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt6-pdf
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
_qtver=6.9.0
pkgver=${_qtver/-/}
pkgrel=1
pkgdesc='QtPdf module (mingw-w64)'
arch=(any)
mingw_arch=('ucrt64' 'clang64')
url='https://www.qt.io'
msys2_repository_url="https://code.qt.io/cgit/qt/qtwebengine.git"
msys2_references=(
  'archlinux: qt6-webengine'
  "cpe: cpe:/a:qt:qt"
)
license=('spdx:LGPL-3.0-only WITH Qt-GPL-exception-1.0 AND BSD-3-Clause AND GFDL-1.3-no-invariants-only AND GPL-2.0-only AND GPL-3.0-only')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-qt6-positioning"
             "${MINGW_PACKAGE_PREFIX}-qt6-webchannel"
             "${MINGW_PACKAGE_PREFIX}-qt6-websockets"
             "${MINGW_PACKAGE_PREFIX}-nodejs"
             "${MINGW_PACKAGE_PREFIX}-lcms2"
             "${MINGW_PACKAGE_PREFIX}-libevent"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-libtiff"
             "${MINGW_PACKAGE_PREFIX}-libwebp"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-minizip"
             "${MINGW_PACKAGE_PREFIX}-nss"
             "${MINGW_PACKAGE_PREFIX}-openjpeg2"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-html5lib"
             "${MINGW_PACKAGE_PREFIX}-re2"
             "${MINGW_PACKAGE_PREFIX}-snappy"
             "${MINGW_PACKAGE_PREFIX}-gperf"
             "rsync")
_pkgfn="qtwebengine-everywhere-src-${_qtver}"
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/submodules/${_pkgfn}.tar.xz"
        "001-use-system-libs.patch"
        "002-use-mingw-python3.patch")
sha256sums=('2b33d1c85e85ed58729db228448f92105ab746ffdc9b98f0c4e3bf00b789789e'
            'b508b7888c5cb50f9b42c6acd9762872d7a29e51c482adffd2163b8af5975c84'
            '5c24c1c87d797579e0ac82852b8613e3672fcc218b6787afaa6d9603065aec7b')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_pkgfn}
  apply_patch_with_msg \
    001-use-system-libs.patch \
    002-use-mingw-python3.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  PYTHON3_PATH=${MINGW_PREFIX}/bin \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    --log-level=STATUS \
    -GNinja \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DQT_FEATURE_qtwebengine_build=OFF \
    -DQT_FEATURE_webengine_system_icu=ON \
    -DQT_FEATURE_webengine_system_re2=ON \
    -DQT_FEATURE_system_png=OFF \
    ../${_pkgfn}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package_qt6-pdf() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-declarative")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  # Seperate debug-info files
  rsync -armR --remove-source-files --include="*/" --include="*.debug" --exclude="*" --prune-empty-dirs "${pkgdir}"/.${MINGW_PREFIX} "${srcdir}"/${MSYSTEM}-debug/

  install -d "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
  install -Dm644 $_pkgfn/LICENSES/* -t "$pkgdir${MINGW_PREFIX}"/share/licenses/${_realname}
}

package_qt6-pdf-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base-debug"
           "${MINGW_PACKAGE_PREFIX}-${_realname}")
  groups=("${MINGW_PACKAGE_PREFIX}-qt6-debug")
  options=('!strip')

  cp -rf "${srcdir}"/${MSYSTEM}-debug${MINGW_PREFIX} "${pkgdir}"/
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
